package lib.Com;

import java.io.File;

import lib.Base.Friend;
import lib.Base.Message;
import lib.Base.Post;
import lib.Base.Profile;
import lib.Base.PublicGroups;
import lib.Base.Request;
import lib.Common.Path;
import lib.gui.GuiChat;
import lib.XML.XMLParser;
import lib.XML.XMLWriter;

public class ObjectHandler extends Thread {
	/*
	 * Class which receive's different object's from the tcp server and choose
	 * the action which is performed by the type of the object
	 */
	private Object currObj;
	private KnownClient client;

	public ObjectHandler(Object currObject, KnownClient client) {

		this.currObj = currObject;
		this.client = client;
	}

	@Override
	public void run() {
		if (currObj != null) {
			handleobject(currObj);
		}
	}

	private void handleobject(Object obj) {

		if (currObj instanceof Message) {
			handleMessage((Message) obj);
		} else if (currObj instanceof Post) {
			handlePost((Post) obj);
		} else if (currObj instanceof Profile) {
			handleProfile((Profile) obj);
		} else if (currObj instanceof PublicGroups) {
			handlePublicGroups((PublicGroups) obj);
		} else if (currObj instanceof Request) {
			handleRequest((Request) obj);
		}
	}

	private void handleMessage(Message msgObj) {
		for (String tmpBUser : Container.settings.getUBlockList()) {
			if (msgObj.getUserId() == tmpBUser) {
				return;
			}
		}
		if (Container.settings.getCAllowed() == 0) {
			GuiChat.sendToChatWindow(msgObj, client.getID());
		} else if (Container.settings.getCAllowed() == 1) {
			for (Friend tmpFriend : Container.friendList) {
				if (tmpFriend.getUserId() == msgObj.getUserId()) {
					GuiChat.sendToChatWindow(msgObj, client.getID());
					break;
				}
			}
		} else {
			return;
		}

	}

	private void handlePost(Post postObj) {
		for (Friend tmpFriend : Container.friendList) {
			if (tmpFriend.getUserId() == client.getID()) {
				// sende kram zum live ticker
				break;
			}
		}
	}

	private void handleProfile(Profile profileObj) {
		File f = new File(Path.XML_PATH_PROFILES + profileObj.getUserId()
				+ ".xml");
		if (f.exists()) {
			Profile tmpProfile = new XMLParser()
					.ParseProfile(Path.XML_PATH_PROFILES
							+ profileObj.getUserId() + ".xml");
			if (profileObj.getLastChange() > tmpProfile.getLastChange()) {
				try {
					f.delete();
					new XMLWriter().WriteProfile(profileObj,
							Path.XML_PATH_PROFILES + profileObj.getUserId()
									+ ".xml");
				} catch (Exception e) {
				}
			}
		} else {
			try {
				new XMLWriter().WriteProfile(profileObj, Path.XML_PATH_PROFILES
						+ profileObj.getUserId() + ".xml");
			} catch (Exception e) {
			}
		}
	}

	private void handlePublicGroups(PublicGroups pGroupsObj) {
		File f = new File(Path.XML_PATH_MAIN + "publicGroups.xml");
		if (f.exists()) {
			PublicGroups publicGroups = new XMLParser()
					.ParsePublicGroups(Path.XML_PATH_MAIN + "publicGroups.xml");
			if (pGroupsObj.getTimestamp() > publicGroups.getTimestamp()) {
				try {
					f.delete();
					new XMLWriter().WritePublicGroups(pGroupsObj,
							Path.XML_PATH_MAIN + "publicGroups.xml");
				} catch (Exception e) {
				}
			}
		} else {
			try {
				new XMLWriter().WritePublicGroups(pGroupsObj,
						Path.XML_PATH_MAIN + "publicGroups.xml");
			} catch (Exception e) {
			}
		}
	}

	private void handleRequest(Request requestObj) {
		if (!Container.settings.getPSilent()) {
			for (String tmpBUser : Container.settings.getUBlockList()) {
				if (requestObj.getRequestUser() == tmpBUser) {
					return;
				}
			}
			if (requestObj.getRequestObject1().equals("profile")) {
				File f = new File(Path.XML_PATH_PROFILES
						+ requestObj.getRequestObject2() + ".xml");
				if (f.exists()) {
					if (Container.settings.getPVisible() == 2) {
						return;
					} else if (Container.settings.getPVisible() == 1) {
						for (Friend tmpFriend : Container.friendList) {
							if (tmpFriend.getUserId() == requestObj
									.getRequestObject2()) {
								Profile tmpProfile = new XMLParser()
										.ParseProfile(Path.XML_PATH_PROFILES
												+ requestObj
														.getRequestObject2()
												+ ".xml");
								if (tmpProfile.getLastChange() > requestObj
										.getTimestamp()) {
									new Client(client, tmpProfile).start();
								} else {
									new Client(client, new Request(tmpProfile
											.getLastChange(), Container.ID,
											"profile", requestObj
													.getRequestObject2()));
								}
								break;
							}
						}
					} else if (Container.settings.getPVisible() == 0) {
						Profile tmpProfile = new XMLParser()
								.ParseProfile(Path.XML_PATH_PROFILES
										+ requestObj.getRequestObject2()
										+ ".xml");
						if (tmpProfile.getLastChange() > requestObj
								.getTimestamp()) {
							new Client(client, tmpProfile).start();
						} else {
							new Client(client, new Request(tmpProfile
									.getLastChange(), Container.ID, "profile",
									requestObj.getRequestObject2()));
						}
					}
				} else {
					for (Friend tmpFriend : Container.friendList) {
						if (tmpFriend.getUserId() == requestObj
								.getRequestObject2()) {
							new Client(client, new Request(0, Container.ID,
									"profile", requestObj.getRequestObject2()))
									.start();
							break;
						}
					}
				}
			}
		}
	}
}